package com.zfxt.web;


import com.baomidou.mybatisplus.extension.api.ApiController;

import com.zfxt.biz.service.IUserService;
import com.zfxt.biz.model.User;
import com.zfxt.common.ResultMsg;
import com.zfxt.util.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xuxing
 * @since 2018-08-30
 */
@Controller
@RequestMapping("/user")
public class UserController extends ApiController {
    @Autowired
    private IUserService iUserService;

    @RequestMapping("/user")
    public String getUser(User account ,ModelMap modelmap){
        if(account==null || account.getName()==null){
            return "/user/accounts/register";
        }
        //用户信息验证
        ResultMsg resultMsg=UserHelper.validate(account);
        if(resultMsg.isSuccess() && iUserService.insertUser(account)){
            modelmap.put("email", account.getEmail());
            return "/user/accounts/registerSubmit";
        }else{
            return "redirect:/accounts/register?" + resultMsg.asUrlParams();
        }
    }
        @RequestMapping("accounts/verify")
        public String verify(String key) {
            boolean result = iUserService.enable(key);
            if (result) {
                return "redirect:/index?" + ResultMsg.successMsg("激活成功").asUrlParams();
            } else {
                return "redirect:/user/user?" + ResultMsg.errorMsg("激活失败,请确认链接是否过期");
            }
        }

    @RequestMapping("/home")
    public String getHome(){
        return "homepage/index";
    }
}

